<template>
  <div>
    <header class="register-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">易物校园</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="register-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">账户注册</a>
        </nav>
        <div class="user-box">
          <div class="form">
            <el-form ref="formRef" :model="form" :rules="rules" label-position="right" label-width="100px" status-icon>
              <el-form-item prop="avatar" label="头像">
                <el-upload
                    ref="uploadRef"
                    :action="uploadUrl + '/file/upload/img'"
                    list-type="picture-card"
                    :on-remove="handleRemove"
                    :on-change="handleChange"
                    :on-exceed="handleExceed"
                    :on-success="handleSuccess"
                    :limit="1"
                    :file-list="uploadFileList"
                    style="border-radius: 50px">
                </el-upload>
              </el-form-item>
              <el-form-item prop="username" label="用户名">
                <el-input v-model="form.username"/>
              </el-form-item>
              <el-form-item prop="password" label="密码">
                <el-input v-model="form.password" type="password"/>
              </el-form-item>
              <el-form-item prop="age" label="年龄">
                <el-input v-model="form.age"/>
              </el-form-item>
              <el-form-item prop="gender" label="性别">
                <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%">
                  <el-option label="男" value="1"></el-option>
                  <el-option label="女" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="idCard" label="身份证号">
                <el-input v-model="form.idCard"/>
              </el-form-item>
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="form.email"/>
              </el-form-item>
              <!--              <el-form-item label-width="22px" prop="agree">-->
              <!--                <el-checkbox size="large" v-model="form.agree">-->
              <!--                  我已同意隐私条款和服务条款-->
              <!--                </el-checkbox>-->
              <!--              </el-form-item>-->
              <el-button size="large" class="subBtn" @click="doRegister">点击注册</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </section>

    <footer class="register-footer">
      <div class="container">
<!--        <p>-->
<!--          <a href="javascript:;">关于我们</a>-->
<!--          <a href="javascript:;">帮助中心</a>-->
<!--          <a href="javascript:;">售后服务</a>-->
<!--          <a href="javascript:;">配送与验收</a>-->
<!--          <a href="javascript:;">商务合作</a>-->
<!--          <a href="javascript:;">搜索推荐</a>-->
<!--          <a href="javascript:;">友情链接</a>-->
<!--        </p>-->
        <p>CopyRight &copy; 易物校园</p>
      </div>
    </footer>
  </div>
</template>

<script setup>
import {ref} from 'vue';
import {ElMessage, genFileId} from 'element-plus';
import 'element-plus/theme-chalk/el-message.css';
import {useRouter} from 'vue-router';
import {useUserStore} from '@/stores/userStore';
import {registerApi} from "@/apis/user";

const uploadUrl = import.meta.env.VITE_API_URL;
const userStore = useUserStore();

console.log(uploadUrl);

const form = ref({
  avatar: '',
  username: '',
  password: '',
  age: '',
  gender: '',
  idCard: '',
  email: ''
});

const rules = ref({
  avatar: [{required: true, message: "请上传用户头像"}],
  username: [{required: true, message: "请填写用户姓名"}],
  password: [{required: true, message: "请填写密码"}],
  age: [{required: true, message: "请填写年龄"}],
  gender: [{required: true, message: "请选择性别"}],
  idCard: [{required: true, message: "请填写身份证号"}],
  email: [{required: true, message: "请填写邮箱"}],
  agree: [
    {
      validator: (rule, value, callback) => {
        console.log(value);
        if (value) {
          callback();
        } else {
          callback(new Error('请勾选协议'));
        }
      }
    }
  ]
});

const uploadFileList = ref([]);
const uploadRef = ref(null);

// Function to handle avatar upload success
const handleSuccess = (res, file) => {
  console.log(file);
  console.log(res.data.fileUrl);
  form.value.avatar = res.data.fileUrl; // 直接赋值给form.avatar
};

const handleRemove = (file, fileList) => {
  console.log(file);
  console.log(fileList);
  uploadFileList.value = [];
}

const handleChange = (file, fileList) => {
  // 只保留最新上传的图片
  uploadFileList.value = [file];
};

const handleExceed = (file, fileList) => {
  console.log(file);
  console.log(fileList);
  uploadFileList[0] = file;
  uploadRef.value.handleStart(file);
}

const formRef = new ref(null);
const router = useRouter();
const doRegister = () => {
  const {username, password} = form.value;
  console.log(form.value);
  formRef.value.validate(async (valid) => {
    console.log(valid);
    if (valid) {
      console.log(username, password);
      await registerApi(form.value);
      ElMessage({type: 'success', message: '注册成功'});
      await router.replace({path: '/login'});
    }
  });
};
</script>

<style scoped lang='scss'>
.register-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.register-section {
  background: url('@/assets/images/register-bg.jpg') no-repeat center / cover;
  height: 688px;
  position: relative;

  .wrapper {
    width: 450px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 35px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.register-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.user-box {
  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>
